/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.c.a.a;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.http.util.ByteArrayBuffer;

public class c {
    private static final ByteArrayBuffer a = a(e.a, ": ");
    private static final ByteArrayBuffer b = a(e.a, "\r\n");
    private static final ByteArrayBuffer c = a(e.a, "--");
    private final String d;
    private final Charset e;
    private final String f;
    private final List g;
    private final d h;

    private static ByteArrayBuffer a(Charset paramCharset, String paramString) {
        ByteBuffer localByteBuffer = paramCharset.encode(CharBuffer.wrap(paramString));
        ByteArrayBuffer localByteArrayBuffer = new ByteArrayBuffer(localByteBuffer.remaining());
        localByteArrayBuffer.append(localByteBuffer.array(), localByteBuffer.position(), localByteBuffer.remaining());
        return localByteArrayBuffer;
    }

    private static void a(ByteArrayBuffer paramByteArrayBuffer, OutputStream paramOutputStream) {
        paramOutputStream.write(paramByteArrayBuffer.buffer(), 0, paramByteArrayBuffer.length());
    }

    private static void a(String paramString, Charset paramCharset, OutputStream paramOutputStream) {
        ByteArrayBuffer localByteArrayBuffer = a(paramCharset, paramString);
        a(localByteArrayBuffer, paramOutputStream);
    }

    private static void a(String paramString, OutputStream paramOutputStream) {
        ByteArrayBuffer localByteArrayBuffer = a(e.a, paramString);
        a(localByteArrayBuffer, paramOutputStream);
    }

    private static void a(f paramf, OutputStream paramOutputStream) {
        a(paramf.a(), paramOutputStream);
        a(a, paramOutputStream);
        a(paramf.b(), paramOutputStream);
        a(b, paramOutputStream);
    }

    private static void a(f paramf, Charset paramCharset, OutputStream paramOutputStream) {
        a(paramf.a(), paramCharset, paramOutputStream);
        a(a, paramOutputStream);
        a(paramf.b(), paramCharset, paramOutputStream);
        a(b, paramOutputStream);
    }

    public c(String paramString1, Charset paramCharset, String paramString2, d paramd) {
        if (paramString1 == null)
            throw new IllegalArgumentException("Multipart subtype may not be null");
        if (paramString2 == null)
            throw new IllegalArgumentException("Multipart boundary may not be null");
        this.d = paramString1;
        this.e = ((paramCharset != null) ? paramCharset : e.a);
        this.f = paramString2;
        this.g = new ArrayList();
        this.h = paramd;
    }

    public List a() {
        return this.g;
    }

    public void a(a parama) {
        if (parama == null)
            return;
        this.g.add(parama);
    }

    public String b() {
        return this.f;
    }

    private void a(d paramd, OutputStream paramOutputStream, boolean paramBoolean)
  {
    ByteArrayBuffer localByteArrayBuffer = a(this.e, b());
    Iterator localIterator = this.g.iterator();
    while (localIterator.hasNext())
    {
      a locala = (a)localIterator.next();
      a(c, paramOutputStream);
      a(localByteArrayBuffer, paramOutputStream);
      a(b, paramOutputStream);
      b localb = locala.c();
      Object localObject1;
      Object localObject2;
      switch (1.a[paramd.ordinal()])
      {
      case 1:
        localObject1 = localb.iterator();
        while (((Iterator)localObject1).hasNext())
        {
          localObject2 = (f)((Iterator)localObject1).next();
          a((f)localObject2, paramOutputStream);
        }
        break;
      case 2:
        localObject1 = locala.c().a("Content-Disposition");
        a((f)localObject1, this.e, paramOutputStream);
        localObject2 = locala.b().b();
        if (localObject2 != null)
        {
          f localf = locala.c().a("Content-Type");
          a(localf, this.e, paramOutputStream);
        }
      }
      a(b, paramOutputStream);
      if (paramBoolean)
        locala.b().a(paramOutputStream);
      a(b, paramOutputStream);
    }
    a(c, paramOutputStream);
    a(localByteArrayBuffer, paramOutputStream);
    a(c, paramOutputStream);
    a(b, paramOutputStream);
  }

    public void a(OutputStream paramOutputStream) {
        a(this.h, paramOutputStream, true);
    }

    public long c() {
        long l1 = 0L;
        Object localObject1 = this.g.iterator();
        Object localObject2;
        while (((Iterator) localObject1).hasNext()) {
            localObject2 = (a) ((Iterator) localObject1).next();
            com.qihoo.c.a.a.a.b localb = ((a) localObject2).b();
            long l2 = localb.e();
            if (l2 >= 0L)
                l1 += l2;
            else
                return -1L;
        }
        localObject1 = new ByteArrayOutputStream();
        try {
            a(this.h, (OutputStream) localObject1, false);
            localObject2 = ((ByteArrayOutputStream) localObject1).toByteArray();
            return (l1 + localObject2.length);
        } catch (IOException localIOException) {}
        return -1L;
    }
}